<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【Vue】整理的一些 Vue3 知识点 | Vixcity</title><meta name="keywords" content="Vue"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="整理的一些 Vue3 知识点看完你就基本可以上手搞开发了，本文适合Vue初学者，或者Vue2迁移者，当然还是建议Vue3官网完全过一遍 不适合精通原理，源码的大佬们。 先推荐两个vscode插件Volar首先推荐Volar，使用vscode开发Vue项目的小伙伴肯定都认识Vetur这个神级插件，有了它可以让我们得开发如鱼得水 那么Volar可以理解为Vue3版本的Vetur，代码高亮，语法提示，基">
<meta property="og:type" content="article">
<meta property="og:title" content="【Vue】整理的一些 Vue3 知识点">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/10/13/%E3%80%90Vue%E3%80%91%E6%95%B4%E7%90%86%E7%9A%84%E4%B8%80%E4%BA%9B%20Vue3%20%E7%9F%A5%E8%AF%86%E7%82%B9/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="整理的一些 Vue3 知识点看完你就基本可以上手搞开发了，本文适合Vue初学者，或者Vue2迁移者，当然还是建议Vue3官网完全过一遍 不适合精通原理，源码的大佬们。 先推荐两个vscode插件Volar首先推荐Volar，使用vscode开发Vue项目的小伙伴肯定都认识Vetur这个神级插件，有了它可以让我们得开发如鱼得水 那么Volar可以理解为Vue3版本的Vetur，代码高亮，语法提示，基">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/Vue.jpg">
<meta property="article:published_time" content="2021-10-12T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.326Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/Vue.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/10/13/%E3%80%90Vue%E3%80%91%E6%95%B4%E7%90%86%E7%9A%84%E4%B8%80%E4%BA%9B%20Vue3%20%E7%9F%A5%E8%AF%86%E7%82%B9/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【Vue】整理的一些 Vue3 知识点',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/Vue.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【Vue】整理的一些 Vue3 知识点</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-12T16:00:00.000Z" title="发表于 2021-10-13 00:00:00">2021-10-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.326Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/Vue/">Vue</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>29分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【Vue】整理的一些 Vue3 知识点"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="整理的一些-Vue3-知识点"><a href="#整理的一些-Vue3-知识点" class="headerlink" title="整理的一些 Vue3 知识点"></a>整理的一些 Vue3 知识点</h2><p>看完你就基本可以上手搞开发了，本文适合<code>Vue</code>初学者，或者<code>Vue2</code>迁移者，当然还是建议<code>Vue3</code>官网完全过一遍</p>
<p>不适合精通原理，源码的大佬们。</p>
<h2 id="先推荐两个vscode插件"><a href="#先推荐两个vscode插件" class="headerlink" title="先推荐两个vscode插件"></a>先推荐两个vscode插件</h2><h3 id="Volar"><a href="#Volar" class="headerlink" title="Volar"></a>Volar</h3><p>首先推荐<code>Volar</code>，使用<code>vscode</code>开发<code>Vue</code>项目的小伙伴肯定都认识<code>Vetur</code>这个神级插件，有了它可以让我们得开发如鱼得水</p>
<p>那么<code>Volar</code>可以理解为<code>Vue3</code>版本的<code>Vetur</code>，代码高亮，语法提示，基本上<code>Vetur</code>有的它都有</p>
<h4 id="特色功能"><a href="#特色功能" class="headerlink" title="特色功能"></a>特色功能</h4><p>当然作为新的插件出山，肯定有它独有的功能。</p>
<h5 id="多个根节点编辑器不会报错"><a href="#多个根节点编辑器不会报错" class="headerlink" title="多个根节点编辑器不会报错"></a>多个根节点编辑器不会报错</h5><p><code>Vue3</code>是允许我们有多个根节点的，但是我们如果使用Vetur就会报错，不会影响运行，但是看起来就很烦</p>
<p>所以当我们转向<code>Volar</code>那么就不会出现这个问题了</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131114131.webp" alt="多个根节点"></p>
<h4 id="编辑器分隔"><a href="#编辑器分隔" class="headerlink" title="编辑器分隔"></a>编辑器分隔</h4><p>即便<code>Vue</code>的组件化开发，可以将单文件的代码长度大幅缩短，但还是动辄几百行甚是上千行</p>
<p>那么我们切换<code>template</code>，<code>script</code>和<code>style</code>的时候就要频繁上下翻，虽然有的插件可以直接定位到<code>css</code>，但是你回不去啊！所以这个功能简直是太人性化了。</p>
<p>安装完<code>Volar</code>以后，打开一个<code>.vue</code>文件，看<code>vscode</code>的右上角，有这么一个图标，点一下</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131114070.webp" alt="图片"></p>
<p>它就会自动给你分隔成三个页面，分别对应<code>template</code>，<code>script</code>和<code>style</code>，这样就太舒服了有没有</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131115723.webp" alt="图片"></p>
<h3 id="Vue-3-Snippets"><a href="#Vue-3-Snippets" class="headerlink" title="Vue 3 Snippets"></a>Vue 3 Snippets</h3><p>推荐的第二个插件叫做<code>Vue 3 Snippets</code>，同样的，他也有自己的<code>Vue2</code>版本</p>
<p>它是干什么的呢，可以看一下下面这张图，我只输入了<code>“v3”</code>，它有很多提示，我们就先选择<code>v3computed</code>，选中回车即可</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131116170.webp" alt="图片"></p>
<p>然后它就给自动给我们写了如下代码</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131116715.webp" alt="图片"></p>
<p>是不是超级省事，摸鱼的时间又增加了！</p>
<p>还有更多有趣的使用方式，小伙伴们自行探索吧</p>
<h2 id="创建Vue3项目"><a href="#创建Vue3项目" class="headerlink" title="创建Vue3项目"></a>创建Vue3项目</h2><p>那么正式开始学习我们的<code>Vue3</code>，先从创建项目开始。</p>
<h3 id="使用-vue-cli-创建"><a href="#使用-vue-cli-创建" class="headerlink" title="使用 vue-cli 创建"></a>使用 vue-cli 创建</h3><p>输入下面的命令然后选择配置项进行安装即可，这里注意<code>vue-cli</code>的版本一定要在<code>4.5.0</code>以上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 安装或者升级  </span><br><span class="line">npm install -g @vue/cli  </span><br><span class="line">//查看版本 保证 vue cli 版本在 4.5.0 以上  </span><br><span class="line">vue --version  </span><br><span class="line">// 创建项目  </span><br><span class="line">vue create my-project  </span><br><span class="line">//然后根据提示一步一步傻瓜式操作就行了  </span><br><span class="line">...  </span><br></pre></td></tr></table></figure>

<h3 id="使用-Vite-创建"><a href="#使用-Vite-创建" class="headerlink" title="使用 Vite 创建"></a>使用 Vite 创建</h3><p>都说<code>Vue3.0</code>和<code>Vite2</code>更配，各种优化，各种快，但都不属于本文的内容，本文的目的我们只需要知道它特别好用，怎么用就行了。我这里是多选择了<code>TS</code>，每行都有注释，一目了然。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">// 初始化viete项目  </span><br><span class="line">npm init vite-app &lt;project-name&gt;  </span><br><span class="line">// 进入项目文件夹  </span><br><span class="line"><span class="built_in">cd</span> &lt;project-name&gt;  </span><br><span class="line">// 安装依赖  </span><br><span class="line">npm install  </span><br><span class="line">//启动项目  </span><br><span class="line">npm run dev  </span><br><span class="line">复制代码  </span><br></pre></td></tr></table></figure>

<p>创建完以后我们先来看看入口文件<code>main.ts</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入createApp函数,创建对应的应用,产生应用的实例对象  </span></span><br><span class="line"><span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span><br><span class="line"><span class="comment">// 引入app组件(所有组件的父级组件)  </span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span>;  </span><br><span class="line"><span class="comment">// 创建app应用返回对应的实例对象,调用mount方法进行挂载  挂载到#app节点上去  </span></span><br><span class="line">createApp(App).mount(<span class="string">&#x27;#app&#x27;</span>);  </span><br></pre></td></tr></table></figure>

<p>然后看看根组件<code>app.vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">//Vue2组件中的html模板中必须要有一对根标签,Vue3组件的html模板中可以没有根标签  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">img</span> <span class="attr">alt</span>=<span class="string">&quot;Vue logo&quot;</span> <span class="attr">src</span>=<span class="string">&quot;./assets/logo.png&quot;</span>&gt;</span>  </span><br><span class="line">  <span class="comment">&lt;!-- 使用子级组件 --&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">HelloWorld</span> <span class="attr">msg</span>=<span class="string">&quot;Welcome to Your Vue.js + TypeScript App&quot;</span> /&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="comment">// 这里可以书写TS代码  </span></span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="comment">// defineComponent函数,目的是定义一个组件 内部可以传入一个配置对象  </span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineComponent &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript"><span class="comment">//引入子级组件  </span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> HelloWorld <span class="keyword">from</span> <span class="string">&#x27;./components/HelloWorld.vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="comment">// 暴露出去一个定义好的组件  </span></span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="comment">// 当前组件的名字  </span></span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;App&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="comment">// 注册组件  </span></span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="comment">// 注册一个子级组件  </span></span></span><br><span class="line"><span class="javascript">    HelloWorld,  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;);  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h2 id="Composition-API"><a href="#Composition-API" class="headerlink" title="Composition API"></a>Composition API</h2><p>接下来到了重头戏，<code>Vue3</code>的招牌特性，<strong>Composition API</strong></p>
<p>关于<code>Composition API</code>这里有大佬做的动画演示，极力推荐</p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651567543&idx=1&sn=e4ff9cbf4e17f0e856afac4eb03b44f8&chksm=80256276b752eb60deea09c1d34c1c97ab60d9e9c31132979877d6cdbf0a6b78aada1ffe25c7&scene=21#wechat_redirect">那个忙了一夜的Vue3动画很好，就是太短了</a></p>
<p><code>Composition API</code>可以更方便的抽取共通逻辑，但是不要过于在意逻辑代码复用，以功能提取代码也是一种思路</p>
<p>顺便提一句，<code>Vue3</code>兼容大部分<code>Vue2</code>语法，所以在<code>Vue3</code>中书写<code>Vue2</code>语法是没有问题的（废除的除外），但是既然我们已经升级<code>Vue3</code>了，不建议混合使用，除非一些大型特殊项目需要兼容两个版本</p>
<h3 id="setup"><a href="#setup" class="headerlink" title="setup"></a>setup</h3><p><code>setup</code>是组合<code>Composition API</code>中的入口函数，也是第一个要使用的函数</p>
<p><code>setup</code>只在初始化时执行一次，所有的<code>Composition API</code>函数都在此使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;我执行了&#x27;</span>) <span class="comment">//我执行了  </span></span><br><span class="line">&#125;,  </span><br></pre></td></tr></table></figure>

<p>可以通过<code>console.log</code>看到<code>setup</code>是在<code>beforeCreate</code>生命周期之前执行的(只执行一次)</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">beforeCreate</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;beforeCreate执行了&#x27;</span>);  </span><br><span class="line">  &#125;,  </span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;setup执行了&#x27;</span>);  </span><br><span class="line">    <span class="keyword">return</span> &#123;&#125;;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  <span class="comment">//setup执行了  </span></span><br><span class="line">  <span class="comment">//beforeCreate执行了  </span></span><br></pre></td></tr></table></figure>

<p>由此可以推断出<code>setup</code>执行的时候,组件对象还没有创建,组件实例对象<code>this</code>还不可用,此时<code>this</code>是<code>undefined</code>, 不能通过<code>this</code>来访问<code>data/computed/methods/props</code></p>
<p>返回对象中的属性会与<code>data</code>函数返回对象的属性合并成为组件对象的属性，返回对象中的方法会与<code>methods</code>中的方法合并成功组件对象的方法，如果有重名, <code>setup</code>优先</p>
<p>因为在<code>setup</code>中<code>this</code>不可用，<code>methods</code>中可以访问<code>setup</code>提供的属性和方法, 但在<code>setup</code>方法中不能访问<code>data</code>和<code>methods</code>里的内容，所以还是不建议混合使用</p>
<p><code>setup</code>函数如果返回对象, 对象中的 <strong>属性</strong> 或 <strong>方法</strong> , <strong>模板</strong> 中可以直接使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//templete  </span></span><br><span class="line">&lt;div&gt;&#123;&#123;number&#125;&#125;&lt;/div&gt;  </span><br><span class="line">  </span><br><span class="line"><span class="comment">//JS  </span></span><br><span class="line"><span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">  <span class="keyword">const</span> number = <span class="number">18</span>;  </span><br><span class="line">  <span class="keyword">return</span> &#123;  </span><br><span class="line">    number,  </span><br><span class="line">  &#125;;  </span><br><span class="line">&#125;,  </span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong><code>setup</code>不能是一个<code>async</code>函数: 因为返回值不再是<code>return</code>的对象, 而是<code>promise</code>, 模板中就不可以使用<code>return</code>中返回对象的数据了</p>
<h4 id="setup的参数-props-context"><a href="#setup的参数-props-context" class="headerlink" title="setup的参数(props,context)"></a>setup的参数(props,context)</h4><p><strong>props</strong>: 是一个对象,里面有父级组件向子级组件传递的数据,并且是在子级组件中使用<code>props</code>接收到的所有的属性</p>
<p><strong>context</strong>：上下文对象，可以通过<code>es6</code>语法解构 <code>setup(props, &#123;attrs, slots, emit&#125;)</code></p>
<ul>
<li><p><strong>attrs</strong>: 获取当前组件标签上所有没有通过<code>props</code>接收的属性的对象, 相当于 <code>this.$attrs</code></p>
</li>
<li><p><strong>slots</strong>: 包含所有传入的插槽内容的对象, 相当于 <code>this.$slots</code></p>
</li>
<li><p><strong>emit</strong>: 用来分发自定义事件的函数, 相当于 <code>this.$emit</code></p>
</li>
</ul>
<p>演示<code>attrs</code>和<code>props</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">//父组件  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">child</span> <span class="attr">:msg</span>=<span class="string">&quot;msg&quot;</span> <span class="attr">msg2</span>=<span class="string">&#x27;哈哈哈&#x27;</span> /&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineComponent, ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript"><span class="comment">// 引入子组件  </span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child <span class="keyword">from</span> <span class="string">&#x27;./components/Child.vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;App&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    Child,  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> msg = ref(<span class="string">&#x27;hello,vue3&#x27;</span>);  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      msg,  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;);  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">//子组件  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h2</span>&gt;</span>子组件<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h3</span>&gt;</span>msg:&#123;&#123; msg &#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineComponent &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;Child&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="attr">props</span>: [<span class="string">&#x27;msg&#x27;</span>],  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params">props, &#123;attrs, slots, emit&#125;</span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;props:&#x27;</span>, props);<span class="comment">//msg: &quot;hello,vue3&quot;  </span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(<span class="string">&#x27;attrs:&#x27;</span>, attrs);<span class="comment">//msg2: &quot;哈哈哈&quot;  </span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;&#125;;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;);  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>演示<code>emit</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">//父组件  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">child</span> @<span class="attr">show</span>=<span class="string">&quot;show&quot;</span> /&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> show = <span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      <span class="built_in">console</span>.log(<span class="string">&#x27;name:&#x27;</span>, <span class="string">&#x27;hzw&#x27;</span>);  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      show,  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">//子组件  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;emitFn&#x27;</span>&gt;</span>事件分发<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineComponent &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;Child&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params">props, &#123; emit &#125;</span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> emitFn = <span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      emit(<span class="string">&#x27;show&#x27;</span>);  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      emitFn,  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;);  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h3 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h3><h4 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h4><p>定义一个响应式的数据(一般用来定义一个基本类型的响应式数据<code>Undefined</code>、<code>Null</code>、<code>Boolean</code>、<code>Number</code>和<code>String</code>)</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> xxx = ref(initValue):  </span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：<code>script</code>中操作数据需要使用<code>xxx.value</code>的形式，而模板中不需要添加<code>.value</code></p>
<p>用一个例子来演示：实现一个按钮,点击可以增加数字</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>&#123;&#123;count&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;updateCount&#x27;</span>&gt;</span>增加<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h5 id="在Vue2中"><a href="#在Vue2中" class="headerlink" title="在Vue2中"></a><strong>在Vue2中</strong></h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">  <span class="keyword">return</span> &#123;  </span><br><span class="line">    <span class="attr">conunt</span>: <span class="number">0</span>,  </span><br><span class="line">  &#125;;  </span><br><span class="line">&#125;,  </span><br><span class="line"><span class="attr">methods</span>: &#123;  </span><br><span class="line">  <span class="function"><span class="title">updateCount</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="built_in">this</span>.conunt++;  </span><br><span class="line">  &#125;,  </span><br><span class="line">&#125;,  </span><br></pre></td></tr></table></figure>

<h5 id="在Vue3中"><a href="#在Vue3中" class="headerlink" title="在Vue3中"></a><strong>在Vue3中</strong></h5> <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="comment">// ref用于定义一个响应式的数据，返回的是一个Ref对象，对象中有一个value属性  </span></span><br><span class="line">    <span class="comment">//如果需要对数据进行操作，需要使用该Ref对象的value属性  </span></span><br><span class="line">    <span class="keyword">const</span> count = ref(<span class="number">0</span>);  </span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">updateCount</span>(<span class="params"></span>) </span>&#123;  </span><br><span class="line">      count.value++;  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="keyword">return</span> &#123;  </span><br><span class="line">      count,  </span><br><span class="line">      updateCount,  </span><br><span class="line">    &#125;;  </span><br><span class="line">  &#125;,  </span><br></pre></td></tr></table></figure>

<p>在<code>Vue2</code>中我们通过<code>this.$refs</code>来获取<code>dom</code>节点，<code>Vue3</code>中我们通过<code>ref</code>来获取节点</p>
<p>首先需要在标签上添加<code>ref=&#39;xxx&#39;</code>，然后再<code>setup</code>中定义一个初始值为<code>null</code>的<code>ref</code>类型,名字要和标签的<code>ref</code>属性一致</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> xxx = ref(<span class="literal">null</span>)  </span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：一定要在<code>setup</code>的<code>return</code>中返回，不然会报错。</p>
<p>还是用一个例子来演示:让输入框自动获取焦点</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h2</span>&gt;</span>App<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span>&gt;</span>---  </span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;inputRef&quot;</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; onMounted, ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="comment">/*   </span></span></span><br><span class="line"><span class="comment"><span class="javascript">ref获取元素: 利用ref函数获取组件中的标签元素  </span></span></span><br><span class="line"><span class="comment"><span class="javascript">功能需求: 让输入框自动获取焦点  </span></span></span><br><span class="line"><span class="comment"><span class="javascript">*/</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> inputRef = ref&lt;HTMLElement|<span class="literal">null</span>&gt;(<span class="literal">null</span>)  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript">    onMounted(<span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      inputRef.value &amp;&amp; inputRef.value.focus()  </span></span><br><span class="line"><span class="javascript">    &#125;)  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      inputRef  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h3 id="reactive"><a href="#reactive" class="headerlink" title="reactive"></a>reactive</h3><h4 id="reactive的语法"><a href="#reactive的语法" class="headerlink" title="reactive的语法"></a>reactive的语法</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> proxy = reactive(obj)  </span><br></pre></td></tr></table></figure>

<h4 id="reactive的作用"><a href="#reactive的作用" class="headerlink" title="reactive的作用"></a>reactive的作用</h4><p>定义多个数据的响应式，接收一个普通对象然后返回该普通对象的响应式代理器对象<code>(Proxy)</code>，响应式转换是“深层的”：会影响对象内部所有嵌套的属性,所有的数据都是响应式的。</p>
<h4 id="代码演示"><a href="#代码演示" class="headerlink" title="代码演示"></a>代码演示</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h3</span>&gt;</span>姓名:&#123;&#123;user.name&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h3</span>&gt;</span>年龄:&#123;&#123;user.age&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h3</span>&gt;</span>wife:&#123;&#123;user.wife&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;updateUser&quot;</span>&gt;</span>更新<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"> setup() &#123;  </span><br><span class="line">    const user = reactive(&#123;  </span><br><span class="line">      name: &#x27;hzw&#x27;,  </span><br><span class="line">      age: 18,  </span><br><span class="line">      wife: &#123;  </span><br><span class="line">        name: &#x27;xioaohong&#x27;,  </span><br><span class="line">        age: 18,  </span><br><span class="line">        books: [&#x27;红宝书&#x27;, &#x27;设计模式&#x27;, &#x27;算法与数据结构&#x27;],  </span><br><span class="line">      &#125;,  </span><br><span class="line">    &#125;);  </span><br><span class="line">    const updateUser = () =&gt; &#123;  </span><br><span class="line">      user.name = &#x27;小红&#x27;;  </span><br><span class="line">      user.age += 2;  </span><br><span class="line">      user.wife.books[0] = &#x27;金瓶梅&#x27;;  </span><br><span class="line">    &#125;;  </span><br><span class="line">    return &#123;  </span><br><span class="line">      user,  </span><br><span class="line">      updateUser,  </span><br><span class="line">    &#125;;  </span><br><span class="line">  &#125;,  </span><br></pre></td></tr></table></figure>

<h3 id="computed函数"><a href="#computed函数" class="headerlink" title="computed函数"></a>computed函数</h3><p>与<code>Vue2</code>中的<code>computed</code>配置功能一致，返回的是一个<code>ref</code>类型的对象</p>
<p>计算属性的函数中如果只传入一个回调函数 表示的是<code>get</code>操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; computed &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span><br><span class="line"><span class="keyword">const</span> user = reactive(&#123;  </span><br><span class="line">  <span class="attr">firstName</span>: <span class="string">&#x27;韩&#x27;</span>,  </span><br><span class="line">  <span class="attr">lastName</span>: <span class="string">&#x27;志伟&#x27;</span>,  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">const</span> fullName1 = computed(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">  <span class="keyword">return</span> user.firstName + user.lastName;  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">return</span> &#123;  </span><br><span class="line">  user,  </span><br><span class="line">  fullName1,  </span><br><span class="line">&#125;;  </span><br></pre></td></tr></table></figure>

<p>计算属性的函数中可以传入一个对象,可以包含<code>set</code>和<code>get</code>函数,进行读取和修改的操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fullName2 = computed(&#123;  </span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="keyword">return</span> user.firstName + <span class="string">&#x27;_&#x27;</span> + user.lastName;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  <span class="function"><span class="title">set</span>(<span class="params">val: string</span>)</span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> names = val.split(<span class="string">&#x27;_&#x27;</span>);  </span><br><span class="line">    user.firstName = names[<span class="number">0</span>];  </span><br><span class="line">    user.lastName = names[<span class="number">1</span>];  </span><br><span class="line">  &#125;,  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">return</span> &#123;  </span><br><span class="line">  user,  </span><br><span class="line">  fullName2,  </span><br><span class="line">&#125;;  </span><br></pre></td></tr></table></figure>

<h3 id="watch函数"><a href="#watch函数" class="headerlink" title="watch函数"></a>watch函数</h3><p>与<code>Vue2</code>中的<code>watch</code>配置功能一致,</p>
<ul>
<li><p>参数1:要监听的数据</p>
</li>
<li><p>参数2:回调函数</p>
</li>
<li><p>参数3:配置</p>
</li>
</ul>
<h4 id="watch函数的作用"><a href="#watch函数的作用" class="headerlink" title="watch函数的作用"></a>watch函数的作用</h4><p>监视指定的一个或多个响应式数据, 一旦数据变化, 就自动执行监视回调</p>
<p>默认初始时不执行回调, 但可以通过配置<code>immediate</code>为<code>true</code>, 来指定初始时立即执行第一次</p>
<p>通过配置<code>deep</code>为<code>true</code>, 来指定深度监视</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; watch, ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span><br><span class="line"><span class="keyword">const</span> user = reactive(&#123;  </span><br><span class="line">  <span class="attr">firstName</span>: <span class="string">&#x27;韩&#x27;</span>,  </span><br><span class="line">  <span class="attr">lastName</span>: <span class="string">&#x27;志伟&#x27;</span>,  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">const</span> fullName3 = ref(<span class="string">&#x27;&#x27;</span>);  </span><br><span class="line">watch(  </span><br><span class="line">  user,  </span><br><span class="line">  <span class="function">(<span class="params">&#123; firstName, lastName &#125;</span>) =&gt;</span> &#123;  </span><br><span class="line">    fullName3.value = firstName + <span class="string">&#x27;_&#x27;</span> + lastName;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &#123; <span class="attr">immediate</span>: <span class="literal">true</span>, <span class="attr">deep</span>: <span class="literal">true</span> &#125;  </span><br><span class="line">);  </span><br><span class="line"><span class="keyword">return</span> &#123;  </span><br><span class="line">  user,  </span><br><span class="line">  fullName3,  </span><br><span class="line">&#125;;  </span><br></pre></td></tr></table></figure>

<p><code>watch</code>监听多个数据,使用数组</p>
<p><code>watch</code>监听非响应式数据的时候需要使用回调函数的形式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">watch([<span class="function">()=&gt;</span>user.firstName,<span class="function">()=&gt;</span>user.lastName,fullName3],<span class="function">()=&gt;</span>&#123;<span class="built_in">console</span>.log(<span class="string">&#x27;我执行了&#x27;</span>)&#125;)  </span><br></pre></td></tr></table></figure>

<h3 id="watchEffect函数"><a href="#watchEffect函数" class="headerlink" title="watchEffect函数"></a>watchEffect函数</h3><h4 id="watchEffect函数的作用"><a href="#watchEffect函数的作用" class="headerlink" title="watchEffect函数的作用"></a>watchEffect函数的作用</h4><p>监视数据发生变化时执行回调，不用直接指定要监视的数据, 回调函数中使用的哪些响应式数据就监视哪些响应式数据，默认初始时就会执行第一次, 从而可以收集需要监视的数据。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; watchEffect, ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span><br><span class="line"><span class="keyword">const</span> user = reactive(&#123;  </span><br><span class="line">  <span class="attr">firstName</span>: <span class="string">&#x27;韩&#x27;</span>,  </span><br><span class="line">  <span class="attr">lastName</span>: <span class="string">&#x27;志伟&#x27;</span>,  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">const</span> fullName4 = ref(<span class="string">&#x27;&#x27;</span>);  </span><br><span class="line">watchEffect(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">  fullName4.value = user.firstName + <span class="string">&#x27;_&#x27;</span> + user.lastName;  </span><br><span class="line">&#125;);  </span><br><span class="line"><span class="keyword">return</span> &#123;  </span><br><span class="line">  user,  </span><br><span class="line">  fullName4,  </span><br><span class="line">&#125;;  </span><br><span class="line">watchEffect可以实现计算属性set方法  </span><br><span class="line">watchEffect(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> names = fullName3.value.split(<span class="string">&#x27;_&#x27;</span>);  </span><br><span class="line">    user.firstName = names[<span class="number">0</span>];  </span><br><span class="line">    user.lastName = names[<span class="number">1</span>];  </span><br><span class="line">&#125;);  </span><br></pre></td></tr></table></figure>

<h3 id="生命周期对比"><a href="#生命周期对比" class="headerlink" title="生命周期对比"></a>生命周期对比</h3><p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131128078.webp" alt="图片"></p>
<p>微信截图_20210623104108.png</p>
<p><strong>注意</strong>:<code>3.0</code>中的生命周期钩子要比<code>2.X</code>中相同生命周期的钩子要快</p>
<p><code>Composition API</code>还新增了以下调试钩子函数：但是不怎么常用</p>
<ul>
<li><p>onRenderTracked</p>
</li>
<li><p>onRenderTriggered</p>
</li>
</ul>
<h4 id="生命周期代码演示"><a href="#生命周期代码演示" class="headerlink" title="生命周期代码演示"></a>生命周期代码演示</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">  </span><br><span class="line">  onBeforeMount(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onBeforeMount&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">  </span><br><span class="line">  onMounted(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onMounted&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">  </span><br><span class="line">  onBeforeUpdate(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onBeforeUpdate&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">  </span><br><span class="line">  onUpdated(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onUpdated&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">  </span><br><span class="line">  onBeforeUnmount(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onBeforeUnmount&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">  </span><br><span class="line">  onUnmounted(<span class="function">() =&gt;</span> &#123;  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;--onUnmounted&#x27;</span>)  </span><br><span class="line">  &#125;)  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<h3 id="toRefs"><a href="#toRefs" class="headerlink" title="toRefs"></a>toRefs</h3><h4 id="toRefs的作用"><a href="#toRefs的作用" class="headerlink" title="toRefs的作用"></a>toRefs的作用</h4><p>把一个响应式对象转换成普通对象，该普通对象的每个属性都是一个 <code>ref</code></p>
<h4 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h4><p>我们使用<code>reactive</code>创建的对象，如果想在模板中使用，就必须得使用<code>xxx.xxx</code>的形式,如果大量用到的话还是很麻烦的,但是使用<code>es6</code>解构以后,会失去响应式,那么<code>toRefs</code>的作用就体现在这,，利用<code>toRefs</code>可以将一个响应式 <code>reactive</code> 对象的所有原始属性转换为响应式的<code>ref</code>属性。当然小伙伴们可以自行开发更多应用场景。</p>
<h4 id="toRefs的代码演示"><a href="#toRefs的代码演示" class="headerlink" title="toRefs的代码演示"></a>toRefs的代码演示</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    name:&#123;&#123;name&#125;&#125;  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&#x27;ts&#x27;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineComponent, reactive, toRefs &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> defineComponent(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> state = reactive(&#123;  </span></span><br><span class="line"><span class="javascript">      <span class="attr">name</span>: <span class="string">&#x27;hzw&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">    &#125;);  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> state2 = toRefs(state);  </span></span><br><span class="line"><span class="javascript">    <span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      state.name += <span class="string">&#x27;===&#x27;</span>;  </span></span><br><span class="line"><span class="javascript">    &#125;, <span class="number">1000</span>);  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      <span class="comment">//通过toRefs返回的对象,解构出来的属性也是响应式的  </span></span></span><br><span class="line"><span class="javascript">      ...state2,  </span></span><br><span class="line"><span class="javascript">    &#125;;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;);  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h3 id="provide-与-inject"><a href="#provide-与-inject" class="headerlink" title="provide 与 inject"></a>provide 与 inject</h3><h4 id="provide-与-inject的作用"><a href="#provide-与-inject的作用" class="headerlink" title="provide 与 inject的作用"></a>provide 与 inject的作用</h4><p>实现跨层级组件(祖孙)间通信</p>
<h4 id="provide-与-inject的代码演示"><a href="#provide-与-inject的代码演示" class="headerlink" title="provide 与 inject的代码演示"></a>provide 与 inject的代码演示</h4><p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>父组件<span class="tag">&lt;/<span class="name">h1</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>当前颜色: &#123;&#123;color&#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;color=&#x27;red&#x27;&quot;</span>&gt;</span>红<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;color=&#x27;yellow&#x27;&quot;</span>&gt;</span>黄<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;color=&#x27;blue&#x27;&quot;</span>&gt;</span>蓝<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">hr</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">Son</span> /&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; provide, ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Son <span class="keyword">from</span> <span class="string">&#x27;./Son.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;ProvideInject&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    Son  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> color = ref(<span class="string">&#x27;red&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    provide(<span class="string">&#x27;color&#x27;</span>, color)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      color  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>子组件<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">hr</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">GrandSon</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> GrandSon <span class="keyword">from</span> <span class="string">&#x27;./GrandSon.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    GrandSon  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>孙子组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h3</span> <span class="attr">:style</span>=<span class="string">&quot;&#123;color&#125;&quot;</span>&gt;</span>孙子组件: &#123;&#123;color&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; inject &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> color = inject(<span class="string">&#x27;color&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      color  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h2 id="其他特性"><a href="#其他特性" class="headerlink" title="其他特性"></a>其他特性</h2><h3 id="Teleport-瞬移"><a href="#Teleport-瞬移" class="headerlink" title="Teleport(瞬移)"></a>Teleport(瞬移)</h3><h4 id="Teleport的作用"><a href="#Teleport的作用" class="headerlink" title="Teleport的作用"></a>Teleport的作用</h4><p><code>Teleport</code> 提供了一种干净的方法, 让组件的<code>html</code>在父组件界面外的特定标签(很可能是<code>body</code>)下插入显示 换句话说就是可以把 <strong>子组件</strong> 或者 <strong>dom节点</strong> 插入到任何你想插入到的地方去。</p>
<h4 id="Teleport的语法"><a href="#Teleport的语法" class="headerlink" title="Teleport的语法"></a>Teleport的语法</h4><p>使用<code>to</code>属性 引号内使用选择器</p>
 <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">teleport</span> <span class="attr">to</span>=<span class="string">&quot;body&quot;</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">teleport</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<h4 id="Teleport的代码演示"><a href="#Teleport的代码演示" class="headerlink" title="Teleport的代码演示"></a>Teleport的代码演示</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">//父组件  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;father&quot;</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>App<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">modal-button</span>&gt;</span><span class="tag">&lt;/<span class="name">modal-button</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> ModalButton <span class="keyword">from</span> <span class="string">&#x27;./components/ModalButton.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;&#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    ModalButton,  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">//子组件  </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;son&quot;</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;modalOpen = true&quot;</span>&gt;</span>  </span><br><span class="line">      点我打开对话框  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">teleport</span> <span class="attr">to</span>=<span class="string">&quot;body&quot;</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;modalOpen&quot;</span>  </span></span><br><span class="line"><span class="tag">           <span class="attr">class</span>=<span class="string">&quot;looklook&quot;</span>&gt;</span>  </span><br><span class="line">        看看我出现在了哪里  </span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;modalOpen = false&quot;</span>&gt;</span>  </span><br><span class="line">          Close  </span><br><span class="line">        <span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">teleport</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;modal-button&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> modalOpen = ref(<span class="literal">false</span>)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      modalOpen,  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>可以看到在子组件中的<code>looklook</code>元素跑到了<code>body</code>下面,而之前的位置默认出现了两行注释</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131129811.webp" alt="图片"></p>
<h3 id="Suspense-不确定的"><a href="#Suspense-不确定的" class="headerlink" title="Suspense(不确定的)"></a>Suspense(不确定的)</h3><h4 id="Suspense的作用"><a href="#Suspense的作用" class="headerlink" title="Suspense的作用"></a>Suspense的作用</h4><p>它们允许我们的应用程序在等待异步组件时渲染一些后备内容，可以让我们创建一个平滑的用户体验</p>
<h4 id="Suspense的语法"><a href="#Suspense的语法" class="headerlink" title="Suspense的语法"></a>Suspense的语法</h4> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">Suspense</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">v-slot:default</span>&gt;</span>  </span><br><span class="line">      <span class="comment">&lt;!-- 异步组件 --&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">AsyncComp</span> /&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">v-slot:fallback</span>&gt;</span>  </span><br><span class="line">      <span class="comment">&lt;!-- 后备内容 --&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>LOADING...<span class="tag">&lt;/<span class="name">h1</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">Suspense</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p><code>vue3</code>中引入异步组件的方式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> AsyncComp = defineAsyncComponent(<span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./AsyncComp.vue&#x27;</span>))  </span><br></pre></td></tr></table></figure>

<h4 id="Suspense的代码演示"><a href="#Suspense的代码演示" class="headerlink" title="Suspense的代码演示"></a>Suspense的代码演示</h4><p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">Suspense</span>&gt;</span>  </span><br><span class="line">     <span class="comment">&lt;!-- v-slot:defaul可以简写成#defaul --&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">v-slot:default</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">AsyncComp</span>/&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line">    <span class="tag">&lt;<span class="name">template</span> <span class="attr">v-slot:fallback</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>LOADING...<span class="tag">&lt;/<span class="name">h1</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">Suspense</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; defineAsyncComponent &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> AsyncComp = defineAsyncComponent(<span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&#x27;./AsyncComp.vue&#x27;</span>))  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript">  <span class="attr">components</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    AsyncComp,  </span></span><br><span class="line"><span class="javascript">  &#125;  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">h2</span>&gt;</span>AsyncComp22<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;AsyncComp&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  setup () &#123;  </span></span><br><span class="line"><span class="javascript">     <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">       <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">         resolve(&#123;  </span></span><br><span class="line"><span class="javascript">           <span class="attr">msg</span>: <span class="string">&#x27;abc&#x27;</span>  </span></span><br><span class="line"><span class="javascript">         &#125;)  </span></span><br><span class="line"><span class="javascript">       &#125;, <span class="number">2000</span>)  </span></span><br><span class="line"><span class="javascript">     &#125;)  </span></span><br><span class="line"><span class="javascript">  &#125;  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>通过下图可以看到在异步组件加载出来之前,显示的是<code>fallback</code>中的内容</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131131493.gif" alt="图片"></p>
<h3 id="响应式数据的判断"><a href="#响应式数据的判断" class="headerlink" title="响应式数据的判断"></a>响应式数据的判断</h3><h4 id="响应式数据的判断作用"><a href="#响应式数据的判断作用" class="headerlink" title="响应式数据的判断作用"></a>响应式数据的判断作用</h4><ul>
<li><p><strong>isRef</strong>: 检查一个值是否为一个 <code>ref</code> 对象</p>
</li>
<li><p><strong>isReactive</strong>: 检查一个对象是否是由 <code>reactive</code> 创建的响应式代理</p>
</li>
<li><p><strong>isReadonly</strong>: 检查一个对象是否是由 <code>readonly</code> 创建的只读代理</p>
</li>
<li><p><strong>isProxy</strong>: 检查一个对象是否是由 <code>reactive</code> 或者 <code>readonly</code> 方法创建的代理</p>
</li>
</ul>
<h4 id="响应式数据的判断代码演示"><a href="#响应式数据的判断代码演示" class="headerlink" title="响应式数据的判断代码演示"></a>响应式数据的判断代码演示</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span><br><span class="line">    <span class="keyword">const</span> state1 = ref(<span class="number">1</span>);  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;isref:&#x27;</span>, isRef(state1));<span class="comment">//isref: true  </span></span><br><span class="line">    <span class="keyword">const</span> state2 = reactive(&#123;&#125;);  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;isReactive:&#x27;</span>, isReactive(state2));<span class="comment">//isReactive: true  </span></span><br><span class="line">    <span class="keyword">const</span> state3 = readonly(&#123;&#125;);  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;isReadonly:&#x27;</span>, isReadonly(state3));<span class="comment">//isReadonly: true  </span></span><br><span class="line">    <span class="keyword">const</span> state4 = reactive(&#123;&#125;);  </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;isProxy:&#x27;</span>, isProxy(state2));<span class="comment">//isProxy: true  </span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;isProxy:&#x27;</span>, isProxy(state4));<span class="comment">//isProxy: true  </span></span><br><span class="line">    <span class="keyword">return</span> &#123;&#125;;  </span><br><span class="line">  &#125;,  </span><br></pre></td></tr></table></figure>

<h3 id="其他不常用特性"><a href="#其他不常用特性" class="headerlink" title="其他不常用特性"></a>其他不常用特性</h3><p>还有很多很多不常用的新特性，我在日常开发中是没有用到的，很多都是用来做优化的，感兴趣的小伙伴们自行去官网查看，或者大佬们可以介绍一下应用场景。</p>
<ul>
<li><p>shallowReactive</p>
</li>
<li><p>shallowRef</p>
</li>
<li><p>readonly</p>
</li>
<li><p>shallowReadonly</p>
</li>
<li><p>markRaw</p>
</li>
<li><p>customRef</p>
</li>
<li><p>…</p>
</li>
</ul>
<h3 id="语法糖"><a href="#语法糖" class="headerlink" title="语法糖"></a>语法糖</h3><p>虽然<code>Composition API</code>用起来已经非常方便了，但是我们还是有很烦的地方，比如</p>
<ul>
<li><p>组件引入了还要注册</p>
</li>
<li><p>属性和方法都要在<code>setup</code>函数中返回，有的时候仅一个<code>return</code>就十几行甚至几十行</p>
</li>
<li><p>…</p>
</li>
<li><p>不想写啊怎么办</p>
</li>
</ul>
<p>好办，<code>Vue3</code>官方提供了<code>script setup</code>语法糖</p>
<p>只需要在<code>script</code>标签中添加<code>setup</code>，组件只需引入不用注册，属性和方法也不用返回，<code>setup</code>函数也不需要，甚至<code>export default</code>都不用写了，不仅是数据，计算属性和方法，甚至是自定义指令也可以在我们的<code>template</code>中自动获得。</p>
<p>但是这么过瘾的语法糖，还是稍微添加了一点点心智负担，因为没有了<code>setup</code>函数，那么<code>props</code>，<code>emit</code>，<code>attrs</code>怎么获取呢，就要介绍一下新的语法了。</p>
<p><code>setup script</code>语法糖提供了三个新的<code>API</code>来供我们使用：<code>defineProps</code>、<code>defineEmit</code>和<code>useContext</code></p>
<ul>
<li><p><strong>defineProps</strong> 用来接收父组件传来的值<code>props</code>。</p>
</li>
<li><p><strong>defineEmit</strong> 用来声明触发的事件表。</p>
</li>
<li><p><strong>useContext</strong> 用来获取组件上下文<code>context</code>。</p>
</li>
</ul>
<h4 id="语法糖的代码演示"><a href="#语法糖的代码演示" class="headerlink" title="语法糖的代码演示"></a>语法糖的代码演示</h4><p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是父组件！<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">Child</span> <span class="attr">msg</span>=<span class="string">&quot;hello&quot;</span>  </span></span><br><span class="line"><span class="tag">           @<span class="attr">child-click</span>=<span class="string">&quot;handleClick&quot;</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child <span class="keyword">from</span> <span class="string">&#x27;./components/Child.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> handleClick = <span class="function">(<span class="params">ctx</span>) =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(ctx)  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">span</span> @<span class="attr">click</span>=<span class="string">&quot;sonClick&quot;</span>&gt;</span>msg: &#123;&#123; props.msg &#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; useContext, defineProps, defineEmit &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> emit = defineEmit([<span class="string">&#x27;child-click&#x27;</span>])  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> ctx = useContext()  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> props = defineProps(&#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">msg</span>: <span class="built_in">String</span>,  </span></span><br><span class="line"><span class="javascript">&#125;)  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> sonClick = <span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">  emit(<span class="string">&#x27;child-click&#x27;</span>, ctx)  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>我们点击一下子组件<img src="https://mmbiz.qpic.cn/mmbiz_png/zPh0erYjkib3WKpiaGhyrvJfOO1gIDEcRdsEqXwEpRtrZ6Aq9l1HjKpo38prOUBictgYv6Dpqia98fRUFQes3SODDQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片">可以看到<code>context</code>被打印了出来，其中的<code>attrs</code>、<code>emit</code>、<code>slots</code>、<code>expose</code>属性和方法依然可以使用。<code>props</code>也可以输出在页面上，事件也成功派发。</p>
<h3 id="其他知识点"><a href="#其他知识点" class="headerlink" title="其他知识点"></a>其他知识点</h3><p>接下来介绍一下我使用<code>Vue3</code>过程中遇到的问题或者小技巧，不全面，想起什么就写什么吧</p>
<h4 id="script-setup语法糖请注意"><a href="#script-setup语法糖请注意" class="headerlink" title="script setup语法糖请注意"></a>script setup语法糖请注意</h4><p>如果在父组件中通过<code>ref=&#39;xxx&#39;</code>的方法来获取子组件实例，子组件使用了<code>script setup</code>语法糖,那么子组件的数据需要用expose的方式导出，否则会因为获取不到数据而报错。</p>
<h4 id="其他知识点的代码演示"><a href="#其他知识点的代码演示" class="headerlink" title="其他知识点的代码演示"></a>其他知识点的代码演示</h4><p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是父组件！<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">Child</span> <span class="attr">ref</span>=<span class="string">&#x27;son&#x27;</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child <span class="keyword">from</span> <span class="string">&#x27;./components/Child.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> son = ref(<span class="literal">null</span>)  </span></span><br><span class="line"><span class="javascript"><span class="built_in">console</span>.log(<span class="string">&#x27;🚀🚀~ son:&#x27;</span>, son)  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br><span class="line">复制代码  </span><br><span class="line">`</span><br><span class="line"></span><br><span class="line">子组件先不使用语法糖</span><br><span class="line"></span><br><span class="line">`<span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    我是子组件&#123;&#123;msg&#125;&#125;  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> &gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> msg = ref(<span class="string">&#x27;hello&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      msg,  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br></pre></td></tr></table></figure>

<p>可以看到是可以获取到我们在子组件中定义的<code>msg</code>属性的</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131132694.webp" alt="图片"><br>现在把子组件换成<code>script setup</code>语法糖再来试一试</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    我是子组件&#123;&#123;msg&#125;&#125;  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> msg = ref(<span class="string">&#x27;hello&#x27;</span>)  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>可以看到现在是获取不到子组件定义的<code>msg</code>属性的</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131132506.webp" alt="图片"></p>
<p>我们可以看看尤大大怎么说</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131132564.webp" alt="图片"></p>
<blockquote>
<p>补充自评论区 <strong>用户2150817567886</strong> setup语法糖定义的组件默认情况下是不对外开发内部调用的，它需要用expose()函数来定义哪些数据允许对外开放，具体内容参考[1]</p>
</blockquote>
<h3 id="Emit派发事件可以对参数进行验证"><a href="#Emit派发事件可以对参数进行验证" class="headerlink" title="Emit派发事件可以对参数进行验证"></a>Emit派发事件可以对参数进行验证</h3><p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是父组件！<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">Child</span> @<span class="attr">sonClick</span>=<span class="string">&#x27;sonClick&#x27;</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child <span class="keyword">from</span> <span class="string">&#x27;./components/Child.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">const</span> sonClick = <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="built_in">console</span>.log(value)  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">  <span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    我是子组件&#123;&#123; msg &#125;&#125;  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;handleClick(1)&quot;</span>&gt;</span>我是按钮1<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;handleClick(2)&quot;</span>&gt;</span>我是按钮2<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  <span class="attr">emits</span>: &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="attr">sonClick</span>: <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (value === <span class="number">1</span>) &#123;  </span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> <span class="literal">true</span>  </span></span><br><span class="line"><span class="javascript">      &#125; <span class="keyword">else</span> &#123;  </span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> <span class="literal">false</span>  </span></span><br><span class="line"><span class="javascript">      &#125;  </span></span><br><span class="line"><span class="javascript">    &#125;,  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params">props, &#123; emit &#125;</span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> msg = ref(<span class="string">&#x27;hello&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> handleClick = <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      emit(<span class="string">&#x27;sonClick&#x27;</span>, value)  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      msg,  </span></span><br><span class="line"><span class="javascript">      handleClick,  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>我们分别点一下按钮1和按钮2，可以看到当我们点了按钮2的时候，控制台会发出警告，但是程序会继续执行，还没想到什么适合的应用场景，但是要知道这个知识点，小伙伴们可以在这搞事情</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131331066.webp" alt="图片"></p>
<h3 id="跨组件通讯mitt-js"><a href="#跨组件通讯mitt-js" class="headerlink" title="跨组件通讯mitt.js"></a>跨组件通讯mitt.js</h3><p><code>Vue2</code>中怎么实现跨组件通讯呢,很多人第一想法就是<code>event bus</code></p>
<p>但是<code>Vue3</code>移除了<code>$on</code>,<code>$once</code>,<code>$off</code>导致不能使用这个方法</p>
<p>但是<code>Vue</code>官方给大家推荐了<code>mitt.js</code>,它的原理就是<code>event bus</code></p>
<h4 id="跨组件通讯mitt-js的代码演示"><a href="#跨组件通讯mitt-js的代码演示" class="headerlink" title="跨组件通讯mitt.js的代码演示"></a>跨组件通讯mitt.js的代码演示</h4><p>先安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i mitt -s  </span><br></pre></td></tr></table></figure>

<p>然后封装一个<code>hook</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//mitt.js  </span></span><br><span class="line"><span class="keyword">import</span> mitt <span class="keyword">from</span> <span class="string">&#x27;mitt&#x27;</span>  </span><br><span class="line"><span class="keyword">const</span> emitter = mitt();  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> emitter;  </span><br></pre></td></tr></table></figure>

<p>父组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是父组件！<span class="tag">&lt;/<span class="name">h2</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">Child1</span> /&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">Child2</span> /&gt;</span>  </span><br><span class="line">  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">setup</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child1 <span class="keyword">from</span> <span class="string">&#x27;./components/Child1.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> Child2 <span class="keyword">from</span> <span class="string">&#x27;./components/Child2.vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件1</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    我是子组件1  </span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#123;&#123;msg&#125;&#125;<span class="tag">&lt;/<span class="name">h1</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref, onUnmounted &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> emitter <span class="keyword">from</span> <span class="string">&#x27;../mitt&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="comment">//初始化  </span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> msg = ref(<span class="string">&#x27;hello&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> changeMsg = <span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      msg.value = <span class="string">&#x27;world&#x27;</span>  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">    <span class="comment">// 监听事件,更新数据  </span></span></span><br><span class="line"><span class="javascript">    emitter.on(<span class="string">&#x27;change-msg&#x27;</span>, changeMsg)  </span></span><br><span class="line"><span class="javascript">    <span class="comment">// 显式卸载  </span></span></span><br><span class="line"><span class="javascript">    onUnmounted(<span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      emitter.off(<span class="string">&#x27;change-msg&#x27;</span>, changeMsg)  </span></span><br><span class="line"><span class="javascript">    &#125;)  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      msg,  </span></span><br><span class="line"><span class="javascript">      changeMsg,  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>子组件2</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span>  </span><br><span class="line">    我是子组件2  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;changeMsg&#x27;</span>&gt;</span>点击修改msg<span class="tag">&lt;/<span class="name">button</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span>  </span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>  </span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> emitter <span class="keyword">from</span> <span class="string">&#x27;../mitt&#x27;</span>  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;  </span></span><br><span class="line"><span class="javascript">  <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>,  </span></span><br><span class="line"><span class="javascript">  </span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> changeMsg = <span class="function">() =&gt;</span> &#123;  </span></span><br><span class="line"><span class="javascript">      emitter.emit(<span class="string">&#x27;change-msg&#x27;</span>)  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> &#123;  </span></span><br><span class="line"><span class="javascript">      changeMsg,  </span></span><br><span class="line"><span class="javascript">    &#125;  </span></span><br><span class="line"><span class="javascript">  &#125;,  </span></span><br><span class="line"><span class="javascript">&#125;  </span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>演示</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202110131333889.gif" alt="图片"></p>
<h3 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h3><p>先看看<code>Vue2</code>自定义指令的钩子</p>
<ul>
<li><p><strong>bind</strong>：当指令绑定在对应元素时触发。只会触发一次。</p>
</li>
<li><p><strong>inserted</strong>：当对应元素被插入到 <code>DOM</code> 的父元素时触发。</p>
</li>
<li><p><strong>update</strong>：当元素更新时，这个钩子会被触发（此时元素的后代元素还没有触发更新）。</p>
</li>
<li><p><strong>componentUpdated</strong>：当整个组件（包括子组件）完成更新后，这个钩子触发。</p>
</li>
<li><p><strong>unbind</strong>：当指令被从元素上移除时，这个钩子会被触发。也只触发一次。</p>
</li>
</ul>
<p>在 <code>Vue3</code> 中，官方为了更有助于代码的可读性和风格统一，把自定义指令的钩子名称改的更像是组件生命周期，尽管他们是两回事</p>
<ul>
<li><p><strong>bind</strong> =&gt; <strong>beforeMount</strong></p>
</li>
<li><p><strong>inserted</strong> =&gt; <strong>mounted</strong></p>
</li>
<li><p><strong>beforeUpdate</strong>: <strong>新的钩子，会在元素自身更新前触发</strong></p>
</li>
<li><p><strong>update</strong> =&gt; <strong>移除</strong>！</p>
</li>
<li><p><strong>componentUpdated</strong> =&gt; <strong>updated</strong></p>
</li>
<li><p><strong>beforeUnmount</strong>: <strong>新的钩子，当元素自身被卸载前触发</strong></p>
</li>
<li><p><strong>unbind</strong> =&gt; <strong>unmounted</strong></p>
</li>
</ul>
<h3 id="过渡动画"><a href="#过渡动画" class="headerlink" title="过渡动画"></a>过渡动画</h3><p>这个没有什么大的改动，只是修改了两个<code>class</code>名字，正是因为没有什么大的改动，导致我曾经在这里栽了大跟头，写完了怎么都不对，后来查官网才知道。</p>
<p>以下是直接引用 <strong>官网的原文</strong></p>
<blockquote>
<ul>
<li><p><strong>v-enter-from</strong>：定义进入过渡的开始状态。在元素被插入之前生效，在元素被插入之后的下一帧移除。</p>
</li>
<li><p><strong>v-enter-active</strong>：定义进入过渡生效时的状态。在整个进入过渡的阶段中应用，在元素被插入之前生效，在过渡/动画完成之后移除。这个类可以被用来定义进入过渡的过程时间，延迟和曲线函数。</p>
</li>
<li><p><strong>v-enter-to</strong>：定义进入过渡的结束状态。在元素被插入之后下一帧生效 (与此同时 v-enter-from 被移除)，在过渡/动画完成之后移除。</p>
</li>
<li><p><strong>v-leave-from</strong>：定义离开过渡的开始状态。在离开过渡被触发时立刻生效，下一帧被移除。</p>
</li>
<li><p><strong>v-leave-active</strong>：定义离开过渡生效时的状态。在整个离开过渡的阶段中应用，在离开过渡被触发时立刻生效，在过渡/动画完成之后移除。这个类可以被用来定义离开过渡的过程时间，延迟和曲线函数。</p>
</li>
<li><p><strong>v-leave-to</strong>：离开过渡的结束状态。在离开过渡被触发之后下一帧生效 (与此同时 v-leave-from 被删除)，在过渡/动画完成之后移除。</p>
</li>
</ul>
</blockquote>
<p>特别注意的是<code>v-enter</code>改成了<code>v-enter-form</code>，<code>v-leave</code>改成了<code>v-leave-from</code>。</p>
<h3 id="其他小知识"><a href="#其他小知识" class="headerlink" title="其他小知识"></a>其他小知识</h3><blockquote>
<p><code>Vue3</code>移除了<code>filter</code><br>获取组件实例方法<code>getCurrentInstance()</code></p>
</blockquote>
<p>这个方法可以获取到当前组件的实例，相当于<code>Vue2</code>中的<code>this</code>最后，毕竟是个人总结，难免会出现纰漏和错误，期待各路大神的补充和纠正。</p>
<p>原文作者的开源项目:</p>
<p><a target="_blank" rel="noopener" href="https://juejin.cn/post/6963945204965441550">https://juejin.cn/post/6963945204965441550</a>“</p>
<h4 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h4><p>[1]</p>
<p><a target="_blank" rel="noopener" href="https://github.com/vuejs/rfcs/pull/210">https://github.com/vuejs/rfcs/pull/210</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">一尾流莺</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://juejin.cn/post/6977004323742220319">https://juejin.cn/post/6977004323742220319</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">转载自掘金作者一尾流莺，如有侵权请联系删除</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/Vue/">Vue</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/Vue.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/10/14/%E3%80%90HTTP%E3%80%91%E9%9D%A2%E8%AF%95%E5%AE%98%EF%BC%9A%E4%BB%80%E4%B9%88%E6%98%AFHTTP%EF%BC%9FHTTP%E5%92%8CHTTPS%E7%9A%84%E5%8C%BA%E5%88%AB%EF%BC%9F/"><img class="prev-cover" src="/my-gitee-blog/img/Http.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【HTTP】面试官：什么是HTTP？HTTP和HTTPS的区别？</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/10/11/%E3%80%90CSS%E3%80%9150%E4%B8%AA%E6%9C%89%E4%BB%B7%E5%80%BC%E7%9A%84CSS%E7%BC%96%E5%86%99%E8%A7%84%E5%88%99%EF%BC%8C%E8%AE%A9%E4%BD%A0%E5%86%99%E5%87%BA%E6%9B%B4%E5%A5%BD%E7%9A%84CSS/"><img class="next-cover" src="/my-gitee-blog/img/CSS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【CSS】50个有价值的CSS编写规则，让你写出更好的CSS</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/09/14/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91%E6%89%8B%E6%91%B8%E6%89%8B%E6%95%99%E4%BD%A0%E8%B0%83%E8%AF%95%20vue-devtools%20%EF%BC%8C%E4%BD%A0%E7%A1%AE%E5%AE%9A%E4%B8%8D%E6%9D%A5%E7%9C%8B%E7%9C%8B%E5%98%9B/" title="【源码阅读】手摸手教你调试 vue-devtools ，你确定不来看看嘛"><img class="cover" src="/my-gitee-blog/img/vue%E7%9B%B8%E5%85%B3.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-14</div><div class="title">【源码阅读】手摸手教你调试 vue-devtools ，你确定不来看看嘛</div></div></a></div><div><a href="/my-gitee-blog/2021/10/15/%E3%80%90NodeJS%E3%80%91Node.js%20+%20Vue%20%E5%AE%9E%E7%8E%B0%20Excel%20%E5%AF%BC%E5%87%BA%E4%B8%8E%E4%BF%9D%E5%AD%98/" title="【NodeJS】Node.js + Vue 实现 Excel 导出与保存"><img class="cover" src="/my-gitee-blog/img/NodeJs.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-15</div><div class="title">【NodeJS】Node.js + Vue 实现 Excel 导出与保存</div></div></a></div><div><a href="/my-gitee-blog/2021/10/22/%E3%80%90%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E3%80%91Vue-release%20%E5%B0%A4%E9%9B%A8%E6%BA%AA%E5%8F%91%E5%B8%83%20Vue.js%20%E7%9A%84%E6%B5%81%E7%A8%8B%E4%BB%A5%E5%8F%8A%E8%87%AA%E5%8A%A8%E5%8C%96%E5%8F%91%E5%B8%83%E5%8A%9F%E8%83%BD%E7%9A%84%E5%AE%9E%E7%8E%B0/" title="【源码阅读】Vue-release 尤雨溪发布 Vue.js 的流程以及自动化发布功能的实现"><img class="cover" src="/my-gitee-blog/img/vue%E7%9B%B8%E5%85%B3.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-22</div><div class="title">【源码阅读】Vue-release 尤雨溪发布 Vue.js 的流程以及自动化发布功能的实现</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/Vue.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>